import { get, post, put, del } from '@/utils/request'

export interface Role {
  id: string
  name: string
  description: string
  permissions: string[]
  createdAt: string
}

export interface Permission {
  id: string
  name: string
  code: string
  module: string
  description: string
  createdAt: string
}

export interface SystemSettings {
  siteName: string
  logo: string
  theme: {
    primary: string
    secondary: string
    accent: string
  }
  security: {
    passwordMinLength: number
    passwordComplexity: boolean
    sessionTimeout: number
    maxLoginAttempts: number
  }
  notification: {
    email: boolean
    desktop: boolean
    sound: boolean
  }
}

export interface RoleParams {
  name?: string
  page: number
  pageSize: number
}

export interface PermissionParams {
  name?: string
  code?: string
  module?: string
  page: number
  pageSize: number
}

export interface CreateRoleParams {
  name: string
  description: string
  permissions: string[]
}

export interface CreatePermissionParams {
  name: string
  code: string
  module: string
  description: string
}

export interface UpdateSettingsParams extends SystemSettings {}

const SystemAPI = {
  roles: {
    list: '/api/roles',
    create: '/api/roles',
    update: (id: string) => `/api/roles/${id}`,
    delete: (id: string) => `/api/roles/${id}`,
  },
  permissions: {
    list: '/api/permissions',
    create: '/api/permissions',
    update: (id: string) => `/api/permissions/${id}`,
    delete: (id: string) => `/api/permissions/${id}`,
  },
  settings: {
    get: '/api/settings',
    update: '/api/settings',
  },
}

export const SystemService = {
  // 角色管理
  async getRoles(params: RoleParams) {
    return get<{ total: number; items: Role[] }>(SystemAPI.roles.list, params)
  },

  async createRole(params: CreateRoleParams) {
    return post<Role>(SystemAPI.roles.create, params)
  },

  async updateRole(id: string, params: CreateRoleParams) {
    return put<Role>(SystemAPI.roles.update(id), params)
  },

  async deleteRole(id: string) {
    return del(SystemAPI.roles.delete(id))
  },

  // 权限管理
  async getPermissions(params: PermissionParams) {
    return get<{ total: number; items: Permission[] }>(SystemAPI.permissions.list, params)
  },

  async createPermission(params: CreatePermissionParams) {
    return post<Permission>(SystemAPI.permissions.create, params)
  },

  async updatePermission(id: string, params: CreatePermissionParams) {
    return put<Permission>(SystemAPI.permissions.update(id), params)
  },

  async deletePermission(id: string) {
    return del(SystemAPI.permissions.delete(id))
  },

  // 系统设置
  async getSettings() {
    return get<SystemSettings>(SystemAPI.settings.get)
  },

  async updateSettings(params: UpdateSettingsParams) {
    return put<SystemSettings>(SystemAPI.settings.update, params)
  },
}
